package forum.services;

import forum.dao.factories.MySQLDAOFactory;
import forum.entities.Category;
import forum.entities.Post;
import forum.entities.Topic;
import forum.entities.User;
import forum.exceptions.CreateExistingEntityException;

import java.math.BigInteger;
import java.util.List;

public class UserService {

    private MySQLDAOFactory factory = new MySQLDAOFactory();

    public void register(String email,String nickname,String password) throws CreateExistingEntityException{
        User user = factory.getUserDAO().create();
        user.setEmail(email);
        user.setNickName(nickname);
        user.setPassword(password);
        factory.getUserDAO().update(user);
    }

    public User login(String email,String password) {
        User foundUser = factory.getUserDAO().findUserByEmail(email);
        if (foundUser == null || !foundUser.getPassword().equals(password)) {
            throw new IllegalArgumentException("incorrect email or password");
        }
        return foundUser;
    }

    public void createCategory(User user,BigInteger currentCategoryId,String newCategoryName) {
        if (user == null) {
            throw new IllegalArgumentException("Unregistered users can't create forum categories");
        }
        Category category = factory.getCategoryDAO().create();
        category.setAuthorId(user.getId());
        category.setCategoryName(newCategoryName);
        category.setParentCategoryId(currentCategoryId);
        try {
            factory.getCategoryDAO().update(category);
        } catch (CreateExistingEntityException e) {
            // TODO auto-generated try-catch block
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    public void createTopic(User user,Category currentCategory,String topicName,String topicStarterMessage) {
        if (user == null) {
            throw new IllegalArgumentException("Unregistered users can't create forum topics");
        }
        Topic topic = factory.getTopicDAO().create();
        topic.setAuthorId(user.getId());
        topic.setParentCategoryId(currentCategory.getId());
        topic.setTopicName(topicName);
        try {
            factory.getTopicDAO().update(topic);
        } catch (CreateExistingEntityException e) {
            // TODO auto-generated try-catch block
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        postMessage(user,topic,topicStarterMessage);
    }

    public void postMessage(User user,Topic topic,String postContains) {
        if (user == null) {
            throw new IllegalArgumentException("Unregistered users can't post messages");
        }
        Post post = factory.getPostDAO().create();
        post.setAuthorId(user.getId());
        post.setTopicId(topic.getId());
        post.setPostContains(postContains);
        try {
            factory.getPostDAO().update(post);
        } catch (CreateExistingEntityException e) {
            // TODO auto-generated try-catch block
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }

    public void deleteTopic(User user, BigInteger topicId) {
        if (user == null) {
            throw new IllegalArgumentException("Unregistered users cannot delete topics");
        }
        if (!factory.getTopicDAO().read(topicId).getAuthorId().equals(user.getId())) {
            throw new IllegalArgumentException("Topic can be deleted only by author");
        }
        factory.getTopicDAO().delete(topicId);
    }

    public Category getCategory(BigInteger categoryId) {
        return factory.getCategoryDAO().read(categoryId);
    }

    public Topic getTopic(BigInteger id) {
        return factory.getTopicDAO().read(id);
    }

    public List<Category> getChildCategories(Category category) {
        return factory.getCategoryDAO().findCategoriesByParentCategoryId(category.getId());
    }

    public List<Topic> getTopics(Category category) {
        return factory.getTopicDAO().findTopicsByParentCategoryId(category.getId());
    }

    public List<Post> getPosts(Topic topic) {
        return factory.getPostDAO().findPostByTopicId(topic.getId());
    }

    public Category getRootCategory() {
        return factory.getCategoryDAO().getRootCategory();
    }

    public User getAuthor(BigInteger id) {
        return factory.getUserDAO().read(id);
    }

}
